package cn.com.guage.dtm.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.apache.velocity.VelocityContext;
import com.alibaba.fastjson.JSONObject;
import cn.com.guage.dtm.common.constant.GenConstants;
import cn.com.guage.dtm.common.utils.DateUtils;
import cn.com.guage.dtm.common.utils.StringUtils;
import cn.com.guage.dtm.generator.config.GenConfig;
import cn.com.guage.dtm.generator.domain.GenTable;
import cn.com.guage.dtm.generator.domain.GenTableColumn;
import cn.com.guage.dtm.mapping.domain.MappingTransTask;

public class VelocityUtils
{
    /** 项目空间路径 */
    private static final String PROJECT_PATH = "main/java";

    /** mybatis空间路径 */
    private static final String MYBATIS_PATH = "main/resources/mapper";

    /** html空间路径 */
    private static final String TEMPLATES_PATH = "main/resources/templates";
    
    /** 默认上级菜单，系统工具 */
    private static final String DEFAULT_PARENT_MENU_ID = "3";

    /**
     * 设置模板变量信息
     * 
     * @return 模板列表
     */
    public static VelocityContext prepareContext(MappingTransTask task)
    {
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("taskSql", task.getTaskSql());
        velocityContext.put("taskCode", task.getTaskCode());
        velocityContext.put("taskName", task.getTaskName());
        velocityContext.put("sysName", task.getSysName());
        velocityContext.put("moduleName", task.getModuleName());
        
        velocityContext.put("tableCode", task.getTableCode());
        velocityContext.put("tableName", task.getTableName());
        
        /**
         * 一些特殊的子对象，分出来用另外的方法设置，这里是示例
         */
        setChangeNameVelocityContext(velocityContext, task);
        
        return velocityContext;
    }

    public static void setChangeNameVelocityContext(VelocityContext context, MappingTransTask task)
    {
        context.put("packageName", task.getTaskLibName());
        context.put("claseName", task.getTaskClassName());
    }

    /**
     * 获取模板信息
     * 
     * @return 模板列表
     */
    public static List<String> getTransJobTemplateList()
    {
        List<String> templates = new ArrayList<String>();
        templates.add("vm/transJob.java.vm");
        return templates;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, MappingTransTask task)
    {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = task.getTaskLibName();
        // 模块名
        String moduleName = task.getModuleName();
        // 大写类名
        String className = task.getTaskClassName();
        // 业务名称
        String businessName = task.getTaskName();

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");

        if (template.contains("java.vm"))
        {
            fileName = StringUtils.format("{}/service/{}.java", javaPath, className);
        }
        else {
        	fileName= "temp.java";
        }
        return fileName;
    }

    /**
     * 获取项目文件路径
     * 
     * @return 路径
     */
    public static String getProjectPath()
    {
        String packageName = GenConfig.getPackageName();
        StringBuffer projectPath = new StringBuffer();
        projectPath.append("main/java/");
        projectPath.append(packageName.replace(".", "/"));
        projectPath.append("/");
        return projectPath.toString();
    }

    /**
     * 获取包前缀
     * 
     * @param packageName 包名称
     * @return 包前缀名称
     */
    public static String getPackagePrefix(String packageName)
    {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
        return basePackage;
    }

    /**
     * 根据列类型获取导入包
     * 
     * @param genTable 业务表对象
     * @return 返回需要导入的包列表
     */
    public static HashSet<String> getImportList(MappingTransTask task)
    {
        HashSet<String> importList = new HashSet<String>();
        if (StringUtils.isNotNull(task))
        {
            importList.add("java.util.List");
        }
        return importList;
    }

    /**
     * 获取权限前缀
     * 
     * @param moduleName 模块名称
     * @param businessName 业务名称
     * @return 返回权限前缀
     */
    public static String getPermissionPrefix(String moduleName, String businessName)
    {
        return StringUtils.format("{}:{}", moduleName, businessName);
    }

    /**
     * 获取上级菜单ID字段
     * 
     * @param options 生成其他选项
     * @return 上级菜单ID字段
     */
    public static String getParentMenuId(JSONObject paramsObj)
    {
        if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID))
        {
            return paramsObj.getString(GenConstants.PARENT_MENU_ID);
        }
        return DEFAULT_PARENT_MENU_ID;
    }

    /**
     * 获取树编码
     * 
     * @param options 生成其他选项
     * @return 树编码
     */
    public static String getTreecode(JSONObject paramsObj)
    {
        if (paramsObj.containsKey(GenConstants.TREE_CODE))
        {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取树父编码
     * 
     * @param options 生成其他选项
     * @return 树父编码
     */
    public static String getTreeParentCode(JSONObject paramsObj)
    {
        if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
        {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取树名称
     * 
     * @param options 生成其他选项
     * @return 树名称
     */
    public static String getTreeName(JSONObject paramsObj)
    {
        if (paramsObj.containsKey(GenConstants.TREE_NAME))
        {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
        }
        return StringUtils.EMPTY;
    }

}